#include <string.h>
#include <stdio.h>
#include "mhscpu.h"
#include "ring_buffer.h"
#include "dma_test.h"
#include "uart_test.h"

extern void SER_Init (void);
extern UART_TypeDef *PRINT_UART; 

void NextTestSetp(void);
void PrintTestList(void);

RingBufferTypeDef ringbuf;

uint32_t flagRece = 0;
uint32_t flagSend = 0;


uint8_t u8USARTGetDate[1024] = {0};
uint32_t u32USARTGetDateIndex = 0;
uint32_t u32USARTGetDateNum = 256;


int main(void)
{
	int32_t tmpItem;

	SYSCTRL_AHBPeriphClockCmd(SYSCTRL_AHBPeriph_DMA, ENABLE);
	SYSCTRL_AHBPeriphResetCmd(SYSCTRL_AHBPeriph_DMA, ENABLE);
	SYSCTRL_APBPeriphClockCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_UART1 | SYSCTRL_APBPeriph_GPIO ,ENABLE);
	SYSCTRL_APBPeriphResetCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_UART1, ENABLE);
	
	GPIO_PinRemapConfig(GPIOA, GPIO_Pin_0|GPIO_Pin_1, GPIO_Remap_0);
//	GPIO_PinRemapConfig(GPIOB, GPIO_Pin_12|GPIO_Pin_13, GPIO_Remap_3);
//	//GPIO_PinRemapConfig(GPIOC, GPIO_Pin_0|GPIO_Pin_1, GPIO_Remap_3);
//	GPIO_PinRemapConfig(GPIOD, GPIO_Pin_4|GPIO_Pin_5, GPIO_Remap_0);
	

	SER_Init();
	
	PrintTestList();
	
	printf("Select Test Item(Using ASSII Number)\n\n");
	
	while(TRUE)
	{
		if(UART_GetLineStatus(PRINT_UART) & UART_LINE_STATUS_RX_RECVD)
		{
			tmpItem = UART_ReceiveData(PRINT_UART);
			switch(tmpItem)
			{
			case'1':
				{
					printf("Please Connect UART1 and UART2,Befor Test\n");
					NextTestSetp();
					printf("Test Resualt:\n");
					UART_DMA_Test();
					printf("\n");
					break;
				}
			case'2':
				{
					printf("Please Connect UART1 and UART2,Befor Test\n");
					NextTestSetp();
					printf("Test Resualt:\n");
					UART_Test();
					printf("\n");
					break;
				}
			}
			PrintTestList();
			
		}
	}
	
END:
	while(1);
}

void PrintTestList(void)
{
	printf("====================MegaHunt SCPU Test===================\n");
	printf("Test Item List:\n");
	printf("1:DMA Test (On Uart)\n");
	printf("2:UART Test\n");
	printf("==========================List End=======================\n\n");
}


void NextTestSetp(void)
{
	int32_t tmpRece;
	printf("Send ASSII \"n\" and Continue Next Step\n\n");
	while(TRUE)
	{
		if(UART_GetLineStatus(PRINT_UART) & UART_LINE_STATUS_RX_RECVD)
		{
			tmpRece = UART_ReceiveData(PRINT_UART);
			if('N' == tmpRece || 'n' == tmpRece)
				break;
		}
	}
}

void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	
	NVIC_SetPriorityGrouping(NVIC_PriorityGroup_0);

	NVIC_InitStructure.NVIC_IRQChannel = UART0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	NVIC_InitStructure.NVIC_IRQChannel = UART1_IRQn;
	NVIC_Init(&NVIC_InitStructure);

//	NVIC_InitStructure.NVIC_IRQChannel = UART2_IRQn;
//	NVIC_Init(&NVIC_InitStructure);
//	
//	NVIC_InitStructure.NVIC_IRQChannel = UART3_IRQn;
//	NVIC_Init(&NVIC_InitStructure);
}

void UART_Configuration(void)
{
	UART_InitTypeDef UART_InitStructure;
	UART_FIFOInitTypeDef UART_FIFOInitStruct;

	UART_StructInit(&UART_InitStructure);
	UART_FIFOStructInit(&UART_FIFOInitStruct);
	
	UART_InitStructure.UART_BaudRate = 9600;
	UART_InitStructure.UART_WordLength = UART_WordLength_8b;
	UART_InitStructure.UART_StopBits = UART_StopBits_1;
	UART_InitStructure.UART_Parity = UART_Parity_No;
	
	
	UART_FIFOInitStruct.FIFO_Enable = ENABLE;
	UART_FIFOInitStruct.FIFO_DMA_Mode = UART_FIFO_DMA_Mode_1;
	UART_FIFOInitStruct.FIFO_RX_Trigger = UART_FIFO_RX_Trigger_1_4_Full;
	UART_FIFOInitStruct.FIFO_TX_Trigger = UART_FIFO_TX_Trigger_1_4_Full;
	UART_FIFOInitStruct.FIFO_TX_TriggerIntEnable = ENABLE;

	UART_Init(UART0, &UART_InitStructure);
	UART_Init(UART1, &UART_InitStructure);
//	UART_Init(UART2, &UART_InitStructure);	
//	UART_Init(UART3, &UART_InitStructure);

//	UART_FIFOInit(UART3, &UART_FIFOInitStruct);
// 	UART_ITConfig(UART3, UART_IT_RX_RECVD, ENABLE);
	
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
	/* User can add his own implementation to report the file name and line number,
	 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while (1)
	{
	}
}
#endif






